package cn.zf233.springsecurityjwt.config.handler;

import cn.zf233.springsecurityjwt.common.R;
import cn.zf233.springsecurityjwt.util.ServletUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zf233 on 2021/7/12
 */
@Component
public class MyFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException e) throws IOException {
        //当框架验证用户信息失败时执行的方法
        String message = e.getMessage();
        ServletUtil.renderString(response, R.createByErrorMessage(StringUtils.isBlank(message) ? "登陆失败" : message));
    }
}
